from lib.api import iot
from lib.config.index import sensor_config
from lib.sensors.rtc.ds3231 import DS3231

config = sensor_config.get_sensor("ds3231")
scl_pin = config["scl"]
sda_pin = config["sda"]
print("ds3231 config:", config)

ds = DS3231(gpio_scl=scl_pin, gpio_sda=sda_pin)


def upload():
    year, month, day, hour, minute, second = ds.DateTime()  # 获取日期、时间
    temp = ds.Temperature()
    year = f"20{year}"
    month = "{:02}".format(month)
    day = "{:02}".format(day)
    hour = "{:02}".format(hour)
    minute = "{:02}".format(minute)
    second = "{:02}".format(second)
    t = f"{year}-{month}-{day} {hour}:{minute}:{second}"
    # print(t)
    print(f"temp: {temp}°C")
    iot.upload_data({"temp_C": temp, "ds3231": t})


if __name__ == "__main__":
    upload()
